package com.liao.controller;

import com.liao.bean.User;
import com.liao.bean.result.Result;
import com.liao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    private static String mycaptcha;

    @PostMapping()
    public Result<User> login(User user, HttpSession session) throws Exception {
        System.out.println("captcha:"+this.mycaptcha);
        String captcha = user.getCaptcha();
        if(mycaptcha.equals(captcha)){
            System.out.println("验证码通过");
            User existUser = userService.queryUserByPhoneAndCaptcha(user.getPhone());
            if (null != existUser){
                System.out.println("账户通过，登录成功！");
                session.setAttribute("existUser",existUser);
                return new Result<>(true,20020,"登录成功");

            }
        }
        System.out.println("验证失败");
        return new Result<>(false,20021,"登录失败");

    }

    @PostMapping(value = "/{captcha}")
    public Result<User>  getCaptcha(@PathVariable(required = true) String captcha, HttpSession session) throws Exception {
        this.mycaptcha = captcha;
        System.out.println(captcha);
        return new Result<>(true,20030,"验证码获取成功");
    }

    @RequestMapping("/toIndex")
    public String toIndex(){
        return "index";
    }

}
